/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ar.com.wholeconsulting.pacc.persistance.database;

import ar.com.wholeconsulting.pacc.persistance.JPASession;
import ar.com.wholeconsulting.pacc.persistance.ModeloParser;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.persistence.EntityTransaction;

/**
 *
 * @author pable
 */
public abstract class Importer {

    private Connection connection;

    public Importer(Connection connection) {
        this.connection = connection;
    }

    public void queryData(String query) throws SQLException {
        try {
            Statement st = connection.createStatement();
            ResultSet rs = st.executeQuery(query);
            EntityTransaction t = null;

            while (rs.next()) {
                try {
                    t = JPASession.getInstance().getManager().getTransaction();
                    t.begin();
                    importData(rs);
                    t.commit();
                } catch (Exception e) {
                    t.rollback();
                }
            }
        } catch (SQLException ex) {
            throw ex;
        } catch (Exception ex) {
            Logger.getLogger(ModeloParser.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    protected abstract void importData(ResultSet rs);

    public abstract void importaData();
}
